package org.example.user.controller;
import com.mybatisflex.core.paginate.Page;
import jakarta.validation.Valid;
import org.example.common.config.Result;
import org.example.common.enums.LogAsync;
import org.example.user.dto.*;
import org.example.user.entity.SysRoleEntity;
import org.example.user.service.ISysRoleService;
import org.example.user.vo.SysRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/role")
@Validated
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    // 创建角色
    @LogAsync("创建角色")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid CreateRoleDTO createRoleDTO) {
        int result = sysRoleService.createRole(createRoleDTO);
        return Result.ok(result);
    }

    // 修改角色信息
    @LogAsync("修改角色")
    @PutMapping("/update")
    public Result update(@RequestBody @Valid UpdateRoleDTO updateRoleDTO) {
        int result = sysRoleService.updateRole(updateRoleDTO);
        return Result.ok(result);
    }

    // 删除角色
    @LogAsync("删除角色")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long roleId) {
        int result = sysRoleService.deleteRole(roleId);
        return Result.ok(result);
    }

    @GetMapping("/list")
    public Result list() {
        return Result.ok(sysRoleService.list());
    }
    // 角色分页查询
    @GetMapping("/pagelist")
    public Result pagelist(@ModelAttribute RoleQueryDTO roleQueryDTO) {
        Page<SysRoleVO> list = sysRoleService.pageList(roleQueryDTO);
        return Result.ok(list);
    }

    // 角色详情查询
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id") Long roleId) {
        SysRoleVO role = sysRoleService.getRoleDetail(roleId);
        return Result.ok(role);
    }

    @GetMapping("/all")
    @LogAsync("查询所有角色")
    public Result getAllRoles() {
        List<SysRoleEntity> roles = sysRoleService.list();
        return Result.ok(roles);
    }

    // 角色权限分配
    @PostMapping("/assignAcl")
    public Result assignAcl(@RequestBody AssignAclDTO assignAclDTO) {
        sysRoleService.assignAcl(assignAclDTO);
        return Result.ok();
    }



}
